
/*
 XMail OSAX -- Copyright (C) 2004, 2005, 2006, 2007 Jean-Baptiste LE STANG osax@lestang.org
 
	
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 */

#include "auth-pop3.h"
#include "libspopc.h"


/*
XMail OSAX -- Copyright (C) 2004, 2005, 2006, 2007 Jean-Baptiste LE STANG osax@lestang.org

	
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "auth-pop3.h"


#include "smtp.h" 
#include "algorithm.h" 
#include "inetsocket.h" 
#include "smtp-string.h"

/**
 * auth_pop3_init() will be called once when the plugin is loaded.
 */
void auth_pop3_init ()    { /* do nothing */ }

/**
 * auth_pop3_cleanup() will be called once when the plugin is 
 * unloaded.
 */
void auth_pop3_cleanup () { /* do nothing */ }

/**
 * auth_pop3_authenticate() is called by libsmtp after a positive
 * response to the EHLO command was received. All stuff
 * needed for authentication should be implemented here.
 * If auth_pop3_authenticate() fails for some reason, -1 must be 
 * returned and an appropriate error message must be set with
 * the function err_set_error(). On success 0 is expected to 
 * be returned.
 *
 * @param srv   The smtp_srv_t object.
 *
 * @returns 0 on success, otherwise -1.
 */
int auth_apop_authenticate (smtp_srv_t * srv){
	popsession* mysession = NULL;
	char * res = NULL;
	res = apopbegin(srv->server,srv->username,srv->password,&mysession);
	popcancel(mysession);
	popend(mysession);
	if(res != NULL){
		smtp_set_error(srv,res,-1);
	}
	if(res != NULL) free(res);
	return (res==NULL)?0:-1;
}
 
int auth_pop3_authenticate (smtp_srv_t * srv){
	popsession* mysession = NULL;
	char * res = NULL;
	res = popbegin(srv->server,srv->username,srv->password,&mysession);
	popcancel(mysession);
	popend(mysession);
	if(res != NULL){
		smtp_set_error(srv,res,-1);
	}
	if(res != NULL) free(res);
	return (res==NULL)?0:-1;
}

/**
 * auth_pop3_read() will be called by libsmtp anytime it reads
 * from the underlying socket. If the SASL mechanism 
 * implements some sort of decryption, this is the place
 * for it.
 * If there is no need for decryption/modifying of the data
 * block, ssl_read() must be called using srv->sck, buf and 
 * bufsiz as parameter values.
 * If auth_pop3_read() fails for some reason, -1 must be returned 
 * and an appropriate error message must be set with
 * the function err_set_error(). On success the number of
 * bytes that were actually stored in buf must be returned.
 *
 * @param srv     The smtp_srv_t object.
 * @param buf     The buffer that is to be filled.
 * @param bufsiz  The number of bytes that should be read at most.
 *
 * @returns The number of bytes that were stored in buf or -1 
 * if an error occured.
 */
int auth_pop3_read (smtp_srv_t * srv, const char * buf, unsigned int bufsiz)
{
    return (ssl_read (&srv->sck, buf, bufsiz));
}

/**
 * auth_pop3_write() will be called by libsmtp anytime it writes
 * to the underlying socket. If the SASL mechanism 
 * implements some sort of encryption, this is the place
 * for it.
 * If there is no need for encryption/modifying of the data
 * block, ssl_write() must be called using srv->sck, buf and 
 * bufsiz as parameter values.
 * If auth_pop3_write() fails for some reason, -1 must be returned 
 * and an appropriate error message must be set with
 * the function err_set_error(). On success the number of
 * bytes that were actually written must be returned.
 *
 * @param srv     The smtp_srv_t object.
 * @param buf     The buffer that is to be filled.
 * @param bufsiz  The number of bytes that should be read at most.
 *
 * @returns The number of bytes actually written or -1 if an error
 * occured.
 */
int auth_pop3_write (smtp_srv_t * srv, const char * buf, unsigned int bufsiz)
{
    return (ssl_write (&srv->sck, buf, bufsiz));
}

